package com.example.hp.zyfypt_jzy322.activity.model;


import com.example.hp.zyfypt_jzy322.activity.bean.ArticleBean;
import com.example.hp.zyfypt_jzy322.activity.iface.ArticleListener;
import com.example.hp.zyfypt_jzy322.activity.iface.Articleiface;
import com.example.hp.zyfypt_jzy322.activity.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by pkx on 2017/4/20.
 */

public class ArticleModel implements Articleiface {
   //1
    private Retrofit retrofit;
    private String BASEURL =
            "http://amicool.neusoft.edu.cn/";

    public ArticleModel() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    /**
     * 获取文章
     * @param mod
     * @param page
     * @param sessionID
     * @param articleListener
     */
    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener articleListener) {
        //2
         ArticleService articleService =
                 retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = articleService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    articleListener.onResponse(response.body());
                }else
                    articleListener.onFail("OnResponseFail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                articleListener.onFail(t.toString());
            }
        });
    }
}
